﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Dapper;
using MdcService.Structure;
using MySql.Data.MySqlClient;

namespace MdcService.DataAccess
{
	public class MariaDataAccess<T>:IHistoryDataAccess<T>
	{
		private string _connectionString1 = "server=localhost;user id=root;password=password";
		private string _connectionString = "server=localhost;user id=root;password=password;database=MDCSerivce";

		private MySqlConnection _dbConnection1;
		private MySqlConnection _dbConnection;

		public MariaDataAccess()
		{
			_dbConnection1 = new MySqlConnection(_connectionString1);
			_dbConnection1.ExecuteAsync("CREATE DATABASE IF NOT EXISTS MDCSerivce default charset utf8 COLLATE utf8_general_ci;")
				.ContinueWith(i =>
				{
					_dbConnection1.Close();
					using (var c = new MySqlConnection(_connectionString))
					{
						c.Execute(@"CREATE TABLE IF NOT EXISTS MachineValue (Id VARCHAR(50),
																			 Name VARCHAR(255),
																			 Path VARCHAR(255),
																			 TimeStamp DATETIME,
																			 LastConnect DATETIME,
																			 LastRead DATETIME,
																			 Failures INT,
																			 ValueJson TEXT);");
					}
				});
		}


		public IEnumerable<T> Data { get; set; }
		public void Insert(T value)
		{
			using (_dbConnection = new MySqlConnection(_connectionString))
			{
				string query = "INSERT INTO MachineValue VALUES(@Id,@Name,@Path,@TimeStamp,@LastConnect,@LastRead,@Failures,@ValueJson)";
				_dbConnection.Execute(query, value);
			}
		}

		public Task<bool> InsertAsync(T value)
		{
			return Task.Run(() =>
			{
				using (_dbConnection = new MySqlConnection(_connectionString))
				{
					string query =
						"INSERT INTO MachineValue VALUES(@Id,@Name,@Path,@TimeStamp,@LastConnect,@LastRead,@Failures,@ValueJson)";
					_dbConnection.Execute(query, value);
				}
				return true;
			});
		}

		public void Insert(IEnumerable<T> values)
		{
			using (_dbConnection = new MySqlConnection(_connectionString))
			{
				string query = "INSERT INTO MachineValue VALUES(@Id,@Name,@Path,@TimeStamp,@LastConnect,@LastRead,@Failures,@ValueJson)";
				_dbConnection.Execute(query, values);
			}
		}

	    List<T> IDataAccess<T>.Select(string command)
	    {
	        throw new System.NotImplementedException();
	    }

	    public Task<bool> InsertAsync(IEnumerable<T> values)
		{
			return Task.Run(() =>
			{
				using (_dbConnection = new MySqlConnection(_connectionString))
				{
					string query =
						"INSERT INTO MachineValue VALUES(@Id,@Name,@Path,@TimeStamp,@LastConnect,@LastRead,@Failures,@ValueJson)";
					_dbConnection.Execute(query, values);
				}
				return true;
			});
		}

		public IEnumerable<T> Select(string command)
		{
			using (_dbConnection = new MySqlConnection(_connectionString))
			{
				string query = "select * from Users where Id=@Id";
				return _dbConnection.Query<T>(query, new {Id = command});
			}
		}

		public Task<IEnumerable<T>> SelectAsync(string command)
		{
			var query = "select * from Users where Id=@Id";
			return _dbConnection.QueryAsync<T>(query, new {Id = command});
		}

		public void Update(IEnumerable<T> values)
		{
			using (_dbConnection = new MySqlConnection(_connectionString))
			{
				string query = "Update MachineValue SET Name=@Name,Path=@Path,TimeStamp=@TimeStamp,LastConnect=@LastConnect," +
				               "LastRead=@LastRead,Failures=@Failures,ValueJson=@ValueJson WHERE Id=@Id";
				_dbConnection.Execute(query, values);
			}
		}

	    public void Update(T value)
	    {
	        throw new System.NotImplementedException();
	    }

	    public Task<bool> UpdateAsync(IEnumerable<T> values)
		{
			return Task.Run(() =>
			{
				string query = "Update MachineValue SET Name=@Name,Path=@Path,TimeStamp=@TimeStamp,LastConnect=@LastConnect," +
				               "LastRead=@LastRead,Failures=@Failures,ValueJson=@ValueJson WHERE Id=@Id";
				_dbConnection.Execute(query, values);
				return true;
			});
		}

		public void Delete(IEnumerable<T> values)
		{
			using (_dbConnection = new MySqlConnection(_connectionString))
			{
				string query = "DELETE FROM MachineValue WHERE Id=@Id";
				_dbConnection.Execute(query, values);
			}
		}

	    public void Delete(T value)
	    {
	        throw new System.NotImplementedException();
	    }

	    public Task<bool> DeleteAsync(IEnumerable<T> values)
		{
			return Task.Run(() =>
			{
				using (_dbConnection = new MySqlConnection(_connectionString))
				{
					string query = "DELETE FROM MachineValue WHERE Id=@Id";
					_dbConnection.Execute(query, values);
				}
				return true;
			});
		}
	}
}
